package com.itcast.elasticsearch.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcast.elasticsearch.bo.QueryGoodsBO;
import com.itcast.elasticsearch.entity.GoodsEntity;
import com.itcast.elasticsearch.mapper.GoodsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author sai
 * @version 1.0
 * @date 2021/8/19 16:28
 */
@Slf4j
@Service
public class GoodsService {

    private GoodsMapper goodsMapper;

    @Autowired
    public void setGoodsMapper(GoodsMapper goodsMapper) {
        this.goodsMapper = goodsMapper;
    }

    /**
     * 商品分页查询
     * @param queryGoodsBO 查询条件
     * @return page info
     */
    public Page<GoodsEntity> queryByPage(QueryGoodsBO queryGoodsBO) {
        if (null == queryGoodsBO
                || null == queryGoodsBO.getCurrent() || queryGoodsBO.getCurrent() < 1
                || null == queryGoodsBO.getSize() || queryGoodsBO.getSize() < 1) {
            log.info("分页信息不能为空");
            throw new RuntimeException("分页信息不能为空");
        }

        // 分页查询
        return goodsMapper.selectPage(
                new Page<>(queryGoodsBO.getCurrent(), queryGoodsBO.getSize()), null);
    }

}